<?php
/**
 * @Author:      zwh
 * @DateTime:    2016-12-26 16:23:51
 * @Description: 用于钣金系统用户的登录登出
 */
class ScsMaintenanceEngineerController extends BaseController {

    /**
     * @Author:      zwh
     * @DateTime:    2016-12-26 16:24:25
     * @Description: 验证token 
     */
    public  function checkToken(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok');

        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $me=$this->verificationMaintenanceEngineerToken(Input::get("token"),$response);
            if(is_null($me)){
                break;
            }
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "checkToken", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /*
     * 技师登陆
     * 
     */
    public function login(){
        $validator = Validator::make(Input::all(),
            array(
                'username' => 'required',
                'passwd' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_args_error_21001");
                break;
            }

            //传入参数
            $username = Input::get('username');
            $passwd = SCUser::make(Input::get('passwd'));

            //验证密码
            $me=MaintenanceEngineers::where('account',$username)->where('passwd',$passwd)->first();
            if(!is_null($me)){
                if($me->status!=1){
                    $response = Lang::get("scs_error.login_user_status_error_21002");
                    break;
                }
                $ms=MaintenanceShops::find($me->type_id);
                if(is_null($ms)){
                    $response = Lang::get("scs_error.scs_user_shop_error_21013");
                    break;
                }
                if($ms->status!=1){
                    $response = Lang::get("scs_error.scs_user_shop_status_error_21014");
                    break;
                }
                if(in_array($ms->title,array(1,2))){
                    $response = Lang::get("sce_error.login_error_1022");
                    break;
                }
                try {
                    MeUserToken::where('me_id',$me->id)->delete();
                    $me_token = new MeUserToken;
                    $me_token->me_id = $me->id;
                    $me_token->token = md5($username.time().rand(0,100));
                    $me_token->created_at = Common::num2DateTime(time());
                    $me_token->out_time_at = Common::num2DateTime(time() + Config::get("scs_user.token_time_out"));
                    $me_token->save();
                } catch (Exception $e) {
                    Log::error("error", array("system" => "scs" ,"event" => "login", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("scs_error.login_create_token_error_21004");
                    break;
                }
            }else{
                $response = Lang::get("scs_error.login_user_userpw_error_21003");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'id' => $me->id,
                    'name' => $me->name,
                    'title' => $me->title,
                    'token' => $me_token->token,
                    'expired' => $me_token->out_time_at,
                    'shop_id' =>$ms->id,
                    'shop_name'=>$ms->name,
                    'shop_address' =>$ms->address,
                    'shop_desc'=>$ms->desc,
                )
            );
        } while (false);
        Log::info("info", array("system" => "scs" ,"event" => "login", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2015-12-28 19:04:14
     * @Description: scs 用户登出
     */
    public function logout(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }
            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $me=$this->verificationMaintenanceEngineerToken(Input::get("token"),$response);
            if(is_null($me)){                
                break;
            }
            try{
                MeUserToken::where('me_id',$me->id)->delete();
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "logout", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.user_token_clean_error_21007");
                break;
            }
            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array()
            );
        } while (false);
        Log::info("info", array("system" => "scs" ,"event" => "logout", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /*
     * 修改密码
     *
     */
    public function modifyPwd(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'passwd' => 'required|regex:/^[1-9]{3,9}$/',
                'old_passwd' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages();
                break;
            }
            $old_passwd=md5(Input::get('old_passwd'));
            $me=$this->verificationMaintenanceEngineerToken(Input::get("token"),$response);
            if(!is_null($me)){
                if($me->passwd != $old_passwd && $me->passwd != strtoupper($old_passwd)){
                    $response = Lang::get("scs_error.user_passwd_error_21015");
                    break;
                }
                if($me->status!=1){
                    $response = Lang::get("scs_error.login_user_status_error_21002");
                    break;
                }
                $me=MaintenanceEngineers::find($me->id);
                $me->passwd=strtoupper(md5(Input::get('passwd')));
                $me->save();
                
            }else{
                $response = Lang::get("scs_error.scs_user_not_exiet21009");
                break;
            }
    
            try{
                MeUserToken::where('me_id',$me->id)->delete();
            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"event" => "modifyPwd", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("scs_error.user_token_clean_error_21007");
                break;
            }
        } while (false);
        Log::info("info", array("system" => "scs" ,"event" => "modifyPwd", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    

}